﻿#include "cameraform.h"
#include "ui_cameraform.h"
#include <QJsonObject>
#include "singleton.h"
#include <QTimer>
#include "myapp.h"
CameraForm::CameraForm(QWidget* parent) :
    QWidget(parent),
    ui(new Ui::CameraForm)
{
    ui->setupUi(this);
    m_timer = new    QTimer(this);
    m_timer->setInterval(500);
    connect(m_timer, &QTimer::timeout, this, [ = ]
    {
        QJsonObject body;
        body.insert("chnId", myApp::current_chnnid);
        Singleton::Instance()->onSendParam("GetOriginImage", body);
    });

}

CameraForm::~CameraForm()
{
    delete ui;
}

void CameraForm::on_btn_getparam_clicked()
{
    QJsonObject body;
    Singleton::Instance()->onSendParam("GetCameraParam", body);
}

void CameraForm::on_btn_setparam_clicked()
{
    QJsonObject body;
    body.insert("expose", ui->led_expose->text().toInt());
    body.insert("gain", ui->led_gain->text().toInt());
    QJsonObject body_1;
    body_1.insert("x", ui->led_roi_x->text().toFloat());
    body_1.insert("y", ui->led_roi_y->text().toFloat());
    body_1.insert("w", ui->led_roi_w->text().toFloat());
    body_1.insert("h", ui->led_roi_h->text().toFloat());
    body.insert("roi", body_1);
    Singleton::Instance()->onSendParam("SetCameraParam", body);
}
void CameraForm::onGetCameraParam(QJsonObject body)
{
    ui->led_expose->setText(QString::number(body.value("expose").toInt()));
    ui->led_gain->setText(QString::number(body.value("gain").toInt()));
    ui->led_roi_x->setText(QString::number(body.value("roi").toObject().value("x").toDouble(), 'f', 10));
    ui->led_roi_y->setText(QString::number(body.value("roi").toObject().value("y").toDouble(), 'f', 10));
    ui->led_roi_w->setText(QString::number(body.value("roi").toObject().value("w").toDouble(), 'f', 10));
    ui->led_roi_h->setText(QString::number(body.value("roi").toObject().value("h").toDouble(), 'f', 10));

}

void CameraForm::onAlarmImage(WsBinHeader header, QByteArray image, AlarmExtern alarmExtern)
{

    ImageHeader imageHeader;
    int imageHeaderLen = sizeof(ImageHeader);
    memcpy(&imageHeader, header.reserved, imageHeaderLen);
    //if(imageHeader.type == WSIMAGE_TYPE::WSIMAGE_TYPE_COAL)return;
    if(imageHeader.type == WSIMAGE_TYPE::WSIMAGE_TYPE_ORIGIN)
        ui->widget_image->setImage(image);

}



void CameraForm::on_btn_getimage_clicked(bool checked)
{
    if(checked)
    {
        if(!m_timer->isActive())
            m_timer->start();
        ui->btn_getimage->setText("停止获取");
    }
    else
    {
        if(m_timer->isActive())
            m_timer->stop();
        ui->btn_getimage->setText("获取图像");
    }
}
